<!DOCTYPE html>
<html lang="zh" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <meta content="IE=edge" http-equiv="X-UA-Compatible">
    <meta content="width=device-width, initial-scale=1, shrink-to-fit=no" name="viewport">
    <meta content="featon inc." name="author">
    <link href="/static/vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">
    <link href="/static/vendor/skin/ftskin.css" rel="stylesheet">
    <title>添加(更新)检测信息</title>
    <style>
    </style>
</head>

<body style="margin-bottom: 60px;">

<div class="ft-subpage-wrapper container-fluid">
    <form action="#" id="form1" method="post" name="form1">
        <div class="row">
            <div class="col-lg-12" id="form_table">
                <fieldset>
                    <legend id="titleText">-添加(更新)检测信息-</legend>
                    <table cellpadding="0" cellspacing="0" class="form-table form-table-border" id="">
                        <tr>
                            <th><span class="f_red">*</span>产品名称</th>
                            <td colspan="3">
                                <select class="form-control" id="cpId" name="cpId">
                                    <option disabled id="cpId_val" selected>--请选择--</option>
                                </select>
                                <span class="cError"></span>
                            </td>
                        </tr>
                        <tr>
                            <th><span class="f_red">*</span>抽样数量</th>
                            <td colspan="3">
                                <input class="form-control" id="cyNum" name="cyNum" placeholder="" type="number"
                                       value="">
                                <span class="cError"></span>
                            </td>
                        </tr>
                        <tr>
                            <th><span class="f_red">*</span>检测机构</th>
                            <td colspan="3">
                                <select class="form-control" id="org" name="org">
                                    <option disabled id="org_val" selected>--请选择--</option>
                                </select>
                                <span class="cError"></span>
                            </td>
                        </tr>
                        <tr>
                            <th><span class="f_red">*</span>检测方法</th>
                            <td colspan="3">
                                <input class="form-control" id="method" name="method" placeholder="" type="text"
                                       value="">
                                <span class="cError"></span>
                            </td>
                        </tr>
                        <tr>
                            <th><span class="f_red">*</span>检测结果</th>
                            <td colspan="3">
                                <input class="form-control" id="result" name="result" placeholder="" type="text"
                                       value="">
                                <span class="cError"></span>
                            </td>
                        </tr>
                        <tr>
                            <th><span class="f_red">*</span>检测报告</th>
                            <td colspan="3">
                                <label data-container="body" data-content="" data-toggle="popover" for="report"
                                       id="input_label"
                                       style="border: 1px solid brown;border-radius: 10px;color: blue;background-color: burlywood;">
                                    <span id="report_label">选择上传报告</span>

                                    <input class="form-control" id="report" name="report" placeholder=""
                                           style="display: none" type="file"
                                           value="">
                                </label>
                                <span class="msg" style="color: red;display: none;line-height: 10px">
                                                !上传会覆盖现有的报告!
                                            </span>
                                <span class="cError"></span>
                            </td>
                        </tr>
                        <tr>
                            <th>备注</th>
                            <td colspan="3">
                                <input class="form-control" id="remark" name="remark" placeholder="" type="text"
                                       value="">
                            </td>
                        </tr>
                    </table>
                </fieldset>
                <div class="bottom-tools-box">
                    <button class="btn btn-info" id="save_but" onclick="addAndUpdateObject()" type="button"><span
                            aria-hidden="true" class="glyphicon glyphicon-floppy-disk"></span> 保存
                    </button>
                    <button class="btn btn-warning" onclick="top.tab_colseOnclick();" type="button"><span
                            aria-hidden="true" class="glyphicon glyphicon-remove-circle"></span> 取消
                    </button>
                </div>

            </div>
        </div>
    </form>
</div>
<input th:id="id" th:value="${id}" type="hidden"/>
<script src="/static/vendor/jquery/jquery-3.3.1.min.js"></script>
<script src="/static/vendor/bootstrap/js/bootstrap.min.js"></script>
<script src="/static/vendor/skin/js/ft-ui.js"></script>
<script>
    $("form input,form select").blur(function () {
        let siblings = $(this).siblings(".cError");
        $(siblings).empty();
        if ($(this).val().trim() === "" || $(this).val() === null) {
            $(siblings).append(`
            <span style="font-size: 12px;font-weight: bold;color: red">不能为空</span>
            `)
            if ($(this).attr("id") !== "remark"){$(this).focus();}
        }
    })

    function iniInfoForm() {
        let validator = $("#form1").validate({
            debug: true,
            rules: {
                // user_realname: {
                //     required: true
                // },
                // dq_yw_name: {
                //     required: true
                // }
            },
            messages: {
                // user_realname: {
                //     required: "执法人员不能为空！"
                // },
                // dq_yw_name: {
                //     required: "所属地区不能为空"
                // }
            },
            errorPlacement: function (error, element) {
                error.appendTo(element.parents('td').find(".cError"));
            },
            submitHandler: function (form) {
            }
        });
    }

    let id = $("#id").val();
    $(function () {
        if (id !== null && id !== '') {
            $.get("/pc/getObjByArgs?id=" + id, function (result) {
                console.log(result)
                $("#cpId_val").val(result[0].productInfo.cpId).text(result[0].productInfo.proName).removeAttr("disabled");
                $("#cyNum").val(result[0].cyNum);
                $("#org_val").text(result[0].org);
                $("#method").val(result[0].method);
                $("#result").val(result[0].result);
                $("#remark").val(result[0].remark);
                $(".msg").show()
            })
        }
    })
    let is_submit = false;
    let url = "";
    if (id !== null && id !== '') {
        url = '/pc/updateProductCheck';
    } else {
        url = '/pc/addProductCheck';
    }
    let formData = new FormData();

    function addAndUpdateObject() {
        let report = document.getElementById('report').files[0];
        if (report ===undefined && id !== null && id !== '') {
            let bean = {
                id: id,
                cpId: $("#cpId").val(),
                cyNum: $("#cyNum").val(),
                org: $("#org").val(),
                method: $("#method").val(),
                result: $("#result").val(),
                remark: $("#remark").val(),
            }
            let submit = true;
            if (id === null || id ===''){
                for (let beanKey in bean) {
                    let beanElement = bean[beanKey];
                    if ((beanElement === null || beanElement === "") && beanKey !== "id" && beanKey !== "remark" && beanKey !== "org"&& beanKey !== "cpId") {
                        submit = false;
                        $("#" + beanKey).siblings(".cError").empty().append(` <span style="font-size: 12px;font-weight: bold;color: red">不能为空</span>`);
                        return;
                    }
                }
            }
            if (submit) {
                if (top.ajax.synclBody(url, bean)) {
                    modelWindow.alert(top.CMSLang.Add_success);
                    top.getCurrentFrameObj().loadListble();
                    top.tab_colseOnclick();
                }else {
                    modelWindow.alert(top.CMSLang.Add_fail);
                }

            }
        } else {
            if (report === undefined &&( id === null || id === '')){
                alert("请选择检测报告")
                return false;
            }else {
                formData.append("report", report)
                $.ajax({
                    url: "/cs/uploadPic",
                    type: "post",
                    data: formData,
                    async: false,
                    cache: false,
                    dataType: 'json',
                    contentType: false,//不设置内容类型
                    processData: false, //不处理数据
                    success: function (result) {
                        console.log(result)
                        if (result !== null) {
                            let bean = {
                                id: id,
                                cpId: $("#cpId").val(),
                                cyNum: $("#cyNum").val(),
                                org: $("#org").val(),
                                method: $("#method").val(),
                                result: $("#result").val(),
                                remark: $("#remark").val(),
                                report: result.report
                            }
                            let submit = true;
                            if (id ===null || id===''){
                                for (let beanKey in bean) {
                                    let beanElement = bean[beanKey];
                                    if ((beanElement === null || beanElement === "") && beanKey !== "id" && beanKey !== "remark" && beanKey !== "org") {
                                        submit = false;
                                        $("#" + beanKey).siblings(".cError").empty().append(`<span style="font-size: 12px;font-weight: bold;color: red">不能为空</span>`);
                                        return;
                                    }
                                }
                            }
                            if (submit) {
                                if (top.ajax.synclBody(url, bean)) {
                                    modelWindow.alert(top.CMSLang.Add_success);
                                    top.getCurrentFrameObj().loadListble();
                                    top.tab_colseOnclick();
                                }else {
                                    modelWindow.alert(top.CMSLang.Add_fail);
                                }
                            }
                        } else {
                            alert("报告上传异常")
                        }
                    },
                    error: function () {
                        alert("提交失败")
                    },

                });
            }

        }
    }

    $("#cpId").click(function () {
        let $that = $(this);
        let url = '/pm/getObjByArgs';
        let children = $($that).children();
        $("#cpId_val").hide();
        if (children.length <= 1) {
            $("#beeId_val").hide();
            $.get(url, function (result) {
                result.forEach((item) => {
                    $($that).append(`
                    <option value="` + item.cpId + `">` + item.proName + `</option>
                `)
                })
            })
        }
    })
    $("#org").click(function () {
        let $that = $(this);
        let url = '/dictCategory/getDictList/org';
        let children = $($that).children();
        if (children.length <= 1) {
            $("#org_val").hide();
            $.get(url, function (result) {
                result.forEach((item) => {
                    $($that).append(`
                    <option value="` + item.dict_code + `">` + item.dict_name + `</option>
                `)
                })
            })
        }
    })

    $("#report").on('change', function () {
        let file = this.files[0];
        let name = file.name;
        let indexOf = name.lastIndexOf('.');
        let fileSuffix = name.slice(indexOf);
        let suffix = ['.doc', '.docx', '.pdf'];
        if (suffix.includes(fileSuffix)) {
            $("#report_label").css('color', '#615e57').text('已选择:' + name);
        } else {
            $("#input_label").attr('data-content', '请确认文件为 word或者pdf')
            $("#report_label").css('color', 'red').text('!文件格式错误!');
            $('#input_label').popover('show');
            $(this).val('');
        }
    })

</script>
</body>

</html>
